package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.Machine;
import com.tbit.main.pojo.MachineFaultLog;
import com.tbit.main.pojo.MachineFaultLogSta;
import com.tbit.main.service.MachineFaultLogService;
import com.tbit.main.service.MachineService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 故障上报日志
 *
 * @author LMY
 * @create 2022-11-17 14:00
 */
@RestController
@RequestMapping("/MachineFaultLog")
public class MachineFaultLogController {

    @Autowired
    private MachineFaultLogService machineFaultLogService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private MachineService machineService;

    /**
     * 分组统计 故障状态
     */
    @RequestMapping("/groupByTime")
    public JsonResult<List<MachineFaultLogSta>> groupByTime(String token, Integer accountId, String startTime, String endTime,
                                                            @RequestParam(value = "faultType", required = false) Integer faultType,
                                                            @RequestParam(value = "regionId", required = false) List<Integer> regionId,
                                                            @RequestParam(value = "machineType", required = false) Integer machineType) {
        AccountUser accountUserToken = tokenService.getAccount(token);
        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<MachineFaultLogSta> machineFaultLogSta = machineFaultLogService.groupByTime(accountId, accountUserToken.getAccountUserType(),
                accountUserToken.getAccountUserId(), faultType, startTime, endTime, regionId, machineType);

        return JsonResult.succ(machineFaultLogSta);
    }

    /**
     * 根据时间查询 故障状态
     */
    @RequestMapping("/getByTime")
    public JsonResult<List<MachineFaultLog>> getByTime(Integer accountId, Integer addManId, String startTime, String endTime,
                                                       @RequestParam(value = "userCode", required = false) String userCode,
                                                       @RequestParam(value = "faultType", required = false) Integer faultType,
                                                       @RequestParam(value = "regionId", required = false) List<Integer> regionId,
                                                       @RequestParam(value = "machineType", required = false) Integer machineType) {
        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<MachineFaultLog> machineFaultLogs = machineFaultLogService.getByTime(accountId, addManId, faultType, startTime, endTime, userCode, regionId, machineType);

        return JsonResult.succ(machineFaultLogs);
    }

    /**
     * 根据故障记录唯一id查询
     */
    @RequestMapping("/getByNO")
    public JsonResult<List<MachineFaultLog>> getByNO(String token, String orderNO) {
        Assert.notNull(orderNO, "System.id.notNull");

        List<MachineFaultLog> machineFaultLogs = machineFaultLogService.getByNO(orderNO);

        return JsonResult.succ(machineFaultLogs);
    }

    /**
     * 根据车辆和手机号查询
     *
     * @param token
     * @param userCode
     * @param phone
     * @return
     */
    @RequestMapping("/getUserCodePhone")
    public JsonResult<List<MachineFaultLog>> getUserCodePhone(String token, Integer accountId, String userCode, String phone, String startTime, String endTime,
                                                              @RequestParam(defaultValue = "1") Integer pageNO, @RequestParam(defaultValue = "10") Integer rowCount) {

        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        if (userCode != null) {
            //查询设备信息
            Machine machine = machineService.getByUseCode(userCode);
            if (machine == null) {
                throw new BaseException("Machine.notExist");
            }
            AccountUser accountUser = tokenService.getAccount(token);
            //设备权限判断
            machineService.auth(accountUser.getAgents(), machine.getMachineId());
        }
        return JsonResult.succ(machineFaultLogService.getUserCodePhone(accountId, userCode, phone, startTime, endTime, rowCount, pageNO));
    }

}
